#!/bin/bash

# Test fixed vnodes feature (format)

. ./common

_need_to_be_root

function setUpDevice
{
	_make_device 0 $((100*1024**2))
	_make_device 1 $((200*1024**2))
	_make_device 2 $((300*1024**2))
}

function setUpAutoNode
{
	SHEEP_OPTIONS='' _start_sheep 0
	SHEEP_OPTIONS='' _start_sheep 1
	SHEEP_OPTIONS='' _start_sheep 2
	_wait_for_sheep 3
}

function setUpAutoCluster
{
	setUpDevice
	setUpAutoNode
	_cluster_format -c 2
}

function setUpFixedNode
{
	SHEEP_OPTIONS='-V 100' _start_sheep 0
	SHEEP_OPTIONS='-V 200' _start_sheep 1
	SHEEP_OPTIONS='-V 300' _start_sheep 2
	_wait_for_sheep 3
}

function setUpFixedCluster
{
	setUpDevice
	setUpFixedNode
	_cluster_format -V -c 2
}

function testFormatAutoCluster
{
	setUpAutoNode

	# format failed with -V
	( _cluster_format -V -c 2 ) && _die

	# format succeeded without -V
	_cluster_format -c 2
	$DOG cluster info -v | _filter_cluster_info

	# reformat failed with -V
	( _cluster_format -V -c 2 ) && _die

	# reformat succeeded without -V
	_cluster_format -c 2

	_cleanup
}

function testFormatFixedCluster
{
	setUpFixedNode

	# format failed without -V
	( _cluster_format -c 2 ) && _die

	# format succeeded with -V
	_cluster_format -V -c 2
	$DOG cluster info -v | _filter_cluster_info

	# format failed without -V
	( _cluster_format -c 2 ) && _die

	# format succeeded with -V
	_cluster_format -V -c 2

	_cleanup
}

function testFormatMixedClusterFailed
{
	_make_device 0 $((100*1024**2))
	_make_device 1 $((200*1024**2))
	_make_device 2 $((300*1024**2))
	_make_device 3 $((400*1024**2))
	SHEEP_OPTIONS=''       _start_sheep 0
	SHEEP_OPTIONS='-V 200' _start_sheep 1
	SHEEP_OPTIONS=''       _start_sheep 2
	SHEEP_OPTIONS='-V 400' _start_sheep 3
	_wait_for_sheep 4

	# format failed with or without -V
	( _cluster_format    -c 2 ) && _die
	( _cluster_format -V -c 2 ) && _die

	_cleanup
}

testFormatAutoCluster
testFormatFixedCluster
testFormatMixedClusterFailed
